package com.ming.sps.server.utils;

import com.ming.sps.server.bean.ParamMap;
import com.ming.sps.server.dao.Dao;

import java.util.HashMap;
import java.util.Map;

public class QueueNumberCreator {

    private static class INSTANCECLASS {
        private final static QueueNumberCreator INSTANCE = new QueueNumberCreator();

        public static QueueNumberCreator getInstance() {
            return INSTANCE;
        }
    }

    public static QueueNumberCreator getInstance() {
        return INSTANCECLASS.getInstance();
    }

    public String createQueueNumber(Dao dao, String seqno) throws Exception {
        ParamMap paramMap = dao.queryOneBySql("select * from t_limit_queuenumber where nkey='" + seqno + "'");
        if(paramMap.size() == 0) {
            Map<String, Object> map = new HashMap<>();
            map.put("NKEY", seqno);
            map.put("VAL", 2);
            dao.insert("t_limit_queuenumber", map);
            return seqno + "00001";
        } else {
            int number = paramMap.getInteger("val");
            Map<String, Object> map = new HashMap<>();
            map.put("NKEY", seqno);
            map.put("VAL", (number + 1));
            dao.update("t_limit_queuenumber", "KEY", map);
            return seqno + getNumberToString(number);
        }
    }

    private String getNumberToString(int number) {
        String result = "";
        int length = (number + "").length();
        if(length < 5) {
            for(int i = 0; i < (5 - length); i++) {
                result += "0";
            }
            result += number;
        } else {
            result = number + "";
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(new QueueNumberCreator().getNumberToString(15));
    }
}
